package com.lottery.ruok.lottrey.ui.component;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.ChooseLotteryActivity;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatOneResponse;

import butterknife.BindView;
import butterknife.OnClick;
import count_down.CountDownManager;
import utils.Utils;

public class PrizeHistoryHeaderItemView extends BaseRelativeLayout {
    @BindView(R.id.left)
    View left;
    @BindView(R.id.lottery_name)
    TextView lotteryName;
    @BindView(R.id.type)
    LinearLayout type;
    @BindView(R.id.tv_prize_time)
    TextView tvPrizeTime;
    @BindView(R.id.no)
    TextView no;
    @BindView(R.id.open)
    TextView open;
    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.prizing)
    TextView prizing;
    @BindView(R.id.prize_detail)
    TextView prizeDetail;
    @BindView(R.id.right)
    View right;
    private LotteryStatOneResponse.ItemListBeans data;
    private LotteryStatOneResponse allData;
    private CountDownManager.CountDownModel countDownModel;

    public PrizeHistoryHeaderItemView(Context context) {
        super(context);
    }

    public PrizeHistoryHeaderItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PrizeHistoryHeaderItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {
    }

    private void initCountDown() {
        countDownModel = new CountDownManager.CountDownModel() {
            @Override
            public void onExecute() {
                if (allData != null && data != null) {
                    if (data.getTime() < 0) {
                        data.setTime(allData.getInterval());
                    } else {
                        data.setTime(data.getTime() - 1);
                    }
                    tvPrizeTime.setText("距下一期开奖:" + Utils.getCountTimeByLong(data.getTime() * 1000));
                }
            }
        };
        CountDownManager.asInstance().register(countDownModel);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        initCountDown();
    }

    @Override
    protected void onDetachedFromWindow() {
        CountDownManager.asInstance().unRegister(countDownModel);
        super.onDetachedFromWindow();
    }

    @Override
    protected void initView(Context context) {
        setContentView(R.layout.prize_detail_item);
    }

    @Override
    protected void load(Context context) {

    }

    public void setData(LotteryStatOneResponse.ItemListBeans data, LotteryStatOneResponse allData) {
        this.data = data;
        this.allData = allData;
        this.data.setTime(allData.getTime());
        updateView(data, allData);

    }

    private void updateView(LotteryStatOneResponse.ItemListBeans data, LotteryStatOneResponse allData) {
        if (data != null && allData != null) {
            no.setText(allData.getIssue_no() + "期开奖");

            if (data.getNameX().equals("排列三") || data.getNameX().equals("福彩3D")) {
                open.setText("每天" + allData.getOpen_time() + "开奖");
            } else {
                open.setText(String.format("已开%s期，剩余%s期", allData.getHad_open_num(), allData.getRemain_num()));
            }

            lotteryName.setText(data.getNameX());
            if (allData.getText() != null && !allData.getText().equals("")) {
                prizeDetail.setText(allData.getText());
                prizeDetail.setVisibility(View.VISIBLE);
            } else {
                prizeDetail.setVisibility(View.GONE);
            }

            if (data.isLeft) {
                left.setVisibility(View.VISIBLE);
                right.setVisibility(View.GONE);
            } else if (data.isRight) {
                left.setVisibility(View.GONE);
                right.setVisibility(View.VISIBLE);
            } else {
                left.setVisibility(View.GONE);
                right.setVisibility(View.GONE);
            }
        }
    }


    @OnClick(R.id.type)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.type:
                Intent intent = new Intent(mContext, ChooseLotteryActivity.class);
               // intent.putParcelableArrayListExtra("lists", allData.getItem_list());
                intent.putExtra("code", data.getCode());
                ((Activity) mContext).startActivityForResult(intent, 100);
                ((Activity) mContext).overridePendingTransition(R.anim.anim_enter_from_bottom, R.anim.activity_stay);
                break;
        }
    }
}
